package com.mindmax.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.mindmax.user.domain.InviteCode;
import com.mindmax.user.service.IInviteCodeService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * InviteCodeController
 * 
 * @author cab
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/tenant/invite-code")
@DataSource(value = DataSourceType.YT_ADMIN)
public class InviteCodeController extends BaseController
{
    @Autowired
    private IInviteCodeService inviteCodeService;



    /**
     * 查询InviteCode列表
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(InviteCode inviteCode)
    {
        startPage();
        List<InviteCode> list = inviteCodeService.selectInviteCodeList(inviteCode);
        return getDataTable(list);
    }

    /**
     * 导出InviteCode列表
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:export')")
    @Log(title = "InviteCode", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InviteCode inviteCode)
    {
        List<InviteCode>      list = inviteCodeService.selectInviteCodeList(inviteCode);
        ExcelUtil<InviteCode> util = new ExcelUtil<InviteCode>(InviteCode.class);
        util.exportExcel(response, list, "InviteCode数据");
    }

    /**
     * 获取InviteCode详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(inviteCodeService.selectInviteCodeById(id));
    }


    /**
     * 生成
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:add')")
    @GetMapping("gen")
    public AjaxResult gen(String tenantId)
    {
        Long userId = getUserId();
        String code  = inviteCodeService.gen(userId, tenantId);
        return success(code);
    }

    /**
     * 新增InviteCode
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:add')")
    @Log(title = "InviteCode", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InviteCode inviteCode)
    {
        return toAjax(inviteCodeService.insertInviteCode(inviteCode));
    }

    /**
     * 修改InviteCode
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:edit')")
    @Log(title = "InviteCode", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InviteCode inviteCode)
    {
        return toAjax(inviteCodeService.updateInviteCode(inviteCode));
    }

    /**
     * 删除InviteCode
     */
    @PreAuthorize("@ss.hasPermi('system:InviteCode:remove')")
    @Log(title = "InviteCode", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inviteCodeService.deleteInviteCodeByIds(ids));
    }
}
